package com.ruoyi.web.service.impl;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.HospitalDisease;
import com.ruoyi.web.mapper.HospitalDiseaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.HospitalMapper;
import com.ruoyi.web.domain.Hospital;
import com.ruoyi.web.service.IHospitalService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 医院Service业务层处理
 * 
 * @author why
 * @date 2025-05-13
 */
@Service
public class HospitalServiceImpl implements IHospitalService 
{
    @Resource
    private HospitalMapper hospitalMapper;

    @Resource
    private HospitalDiseaseMapper hospitalDiseaseMapper;

    /**
     * 查询医院
     * 
     * @param id 医院主键
     * @return 医院
     */
    @Override
    public Hospital selectHospitalById(Long id)
    {
        return hospitalMapper.selectHospitalById(id);
    }

    /**
     * 查询医院列表
     * 
     * @param hospital 医院
     * @return 医院
     */
    @Override
    public List<Hospital> selectHospitalList(Hospital hospital)
    {
        return hospitalMapper.selectHospitalList(hospital);
    }

    /**
     * 新增医院
     * 
     * @param hospital 医院
     * @return 结果
     */
    @Override
    public int insertHospital(Hospital hospital)
    {
        hospital.setCreateTime(DateUtils.getNowDate());
        hospital.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        hospital.setCreateName(ShiroUtils.getSysUser().getUserName());

        hospital.setUpdateTime(DateUtils.getNowDate());
        hospital.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        hospital.setUpdateName(ShiroUtils.getSysUser().getUserName());
        int i = hospitalMapper.insertHospital(hospital);

        if (!Objects.isNull(hospital.getHospitalDiseases())){
            hospital.getHospitalDiseases().forEach(bean -> {
                bean.setHospitalId(hospital.getId());

                bean.setCreateTime(DateUtils.getNowDate());
                bean.setCreateBy(ShiroUtils.getSysUser().getLoginName());
                bean.setCreateName(ShiroUtils.getSysUser().getUserName());

                bean.setUpdateTime(DateUtils.getNowDate());
                bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                bean.setUpdateName(ShiroUtils.getSysUser().getUserName());

                hospitalDiseaseMapper.insertHospitalDisease(bean);
            });
        }


        return i;
    }

    /**
     * 修改医院
     * 
     * @param hospital 医院
     * @return 结果
     */
    @Override
    public int updateHospital(Hospital hospital)
    {
        hospital.setUpdateTime(DateUtils.getNowDate());
        hospital.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        hospital.setUpdateName(ShiroUtils.getSysUser().getUserName());

        int i = hospitalMapper.updateHospital(hospital);

        if (!Objects.isNull(hospital.getHospitalDiseases())){
            hospital.getHospitalDiseases().forEach(bean -> {
                if (!Objects.isNull(bean.getId())){
                    bean.setUpdateTime(DateUtils.getNowDate());
                    bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setUpdateName(ShiroUtils.getSysUser().getUserName());
                    hospitalDiseaseMapper.updateHospitalDisease(bean);
                }else {
                    bean.setHospitalId(hospital.getId());
                    bean.setCreateTime(DateUtils.getNowDate());
                    bean.setCreateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setCreateName(ShiroUtils.getSysUser().getUserName());

                    bean.setUpdateTime(DateUtils.getNowDate());
                    bean.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
                    bean.setUpdateName(ShiroUtils.getSysUser().getUserName());

                    hospitalDiseaseMapper.insertHospitalDisease(bean);
                }
            });
        }

        return i;
    }

    /**
     * 批量删除医院
     * 
     * @param ids 需要删除的医院主键
     * @return 结果
     */
    @Override
    public int deleteHospitalByIds(String ids)
    {
        return hospitalMapper.deleteHospitalByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除医院信息
     * 
     * @param id 医院主键
     * @return 结果
     */
    @Override
    public int deleteHospitalById(Long id)
    {
        return hospitalMapper.deleteHospitalById(id,ShiroUtils.getSysUser().getLoginName());
    }
}
